package nc.bs.ia.ia301.command;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import nc.bs.ia.ia301.AuditSession;
import nc.bs.ia.ia306.command.voucher.CostCreateCommand;
import nc.bs.ia.pub.IAContext;
import nc.vo.ia.bill.BillVO;
import nc.vo.ia.ia306.IARtvoucherVO;
import nc.vo.ia.ia501.IaInoutledgerVO;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFDouble;

public class VoucherOperator
{
  private IAContext context = null;

  private List voucherList = new ArrayList();

  public VoucherOperator(IAContext context)
  {
    this.context = context;
  }

  public void collectVoucherBill_BIDs(AuditSession[] sessions)
  {
    List list = getVoucherBill_BIDList(sessions);
    this.voucherList.addAll(list);
  }

  private List getVoucherBill_BIDList(AuditSession[] sessions) {
    List list = new ArrayList();
    if (!this.context.isCanGenerateSSPZ()) {
      return list;
    }
    int length = sessions.length;
    for (int i = 0; i < length; ++i) {
      AuditSession session = sessions[i];

      int index = session.getCurrentIndex();
      List vos = session.getInoutledgerVOs();
      for (int j = 0; j <= index; ++j) {
        IaInoutledgerVO vo = (IaInoutledgerVO)vos.get(j);
        constructVoucherBill(vo, list);
      }

      List bills = session.getAdjustBills();
      int size = bills.size();
      for (int j = 0; j < size; ++j) {
        BillVO bill = (BillVO)bills.get(j);
        IaInoutledgerVO vo = bill.changeToView()[0];
        constructVoucherBill(vo, list);
      }
    }
    return list;
  }

  private void constructVoucherBill(IaInoutledgerVO vo, List list) {
    UFDouble money = vo.getNmoney();
    UFBoolean rtvouchflag = vo.getBrtvouchflag();
    int priceMode = vo.getFpricemodeflag().intValue();

    if ((rtvouchflag != null) && (rtvouchflag.booleanValue())) {
      return;
    }
    int getmodel = vo.getFdatagetmodelflag().intValue();

    if ((money != null) && (money.doubleValue() != 0.0D) && (getmodel != 12))
    {
      list.add(vo.getCbill_bid());
    }
    else if (priceMode == 5) {
      list.add(vo.getCbill_bid());
    }
    else if (priceMode == 4)
      return;
  }

  public void generateSSPZ(boolean needLock)
  {
    int size = this.voucherList.size();
    if (size == 0) {
      return;
    }
    String[] cbill_bids = new String[size];
    cbill_bids = (String[])(String[])this.voucherList.toArray(cbill_bids);

    IARtvoucherVO vo = new IARtvoucherVO();

    vo.setCorpID(this.context.getCorp());
    vo.setLoginDate(this.context.getAuditDate());
    vo.setUserID(this.context.getAuditUser());
    vo.setLoginYear(this.context.getAuditYear());
    vo.setLoginMonth(this.context.getAuditMonth());
    vo.setBillItemIDs(cbill_bids);

    vo.setMethodId(10);
    CostCreateCommand command = new CostCreateCommand(needLock);
    HashMap data = new HashMap();
    data.put("para", vo);
    command.execute(data);
  }
}